package com.baizhi.service.impl;

import com.baizhi.dto.response.UserResponseDTO;
import com.baizhi.entity.User;
import com.baizhi.exception.CodeEnum;
import com.baizhi.mapper.UserMapper;
import com.baizhi.service.UserService;
import com.baizhi.utils.MD5Util;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 16357
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2024-08-01 14:53:19
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;


    @Override
    public boolean changePassword(Integer userId, String oldPassword, String newPassword) {
        User user = userMapper.selectById(userId);
        if(user == null){//用户未注册
            CodeEnum.user_is_no_regist.throwException();
        }
        //判断原密码输入是否正确
        String md5Old = MD5Util.getMd5Password(oldPassword, user.getSalt());
        if(!md5Old.equals(user.getPassword())){
            CodeEnum.old_password_id_incorrect.throwException(); //原密码输入有误
        }
        //判断新密码和老密码是否一致
        String md5New = MD5Util.getMd5Password(newPassword, user.getSalt());
        if(md5Old.equals(md5New)){
            CodeEnum.password_data_is_not_update.throwException(); //新旧密码一致，无需修改
        }

        //执行修改操作
        user.setPassword(md5New);
        int row = userMapper.updateById(user);
        if(row == 0){
            CodeEnum.password_update_fail.throwException();
        }
        return true;
    }

    @Override
    public boolean updateUser(User user) {
        //1. 判断用户ID是否存在  如果不存在抛出用户未注册异常
        User result = userMapper.selectById(user.getPkUid());
        if(result == null){
            CodeEnum.user_is_no_regist.throwException();
        }

        //2. 判断用户数据是否进行了更新 如果没有更新抛出 用户数据未更新修改失败异常
        //姓名 性别 联系电话  地址   user 中 密码，盐值   都是空   但是result 密码盐值有有值

       // name = "张三"  lisi

        boolean flag = true;

        if(user.getRealName()==null || user.getSex() == null || user.getPhone()==null || user.getAddress()==null){
            CodeEnum.update_userdata_not_is_null.throwException();
        }

        if(!user.getRealName().equals(result.getRealName())){
            flag = false;
        }
        if(!user.getSex().equals(result.getSex())){
            flag = false;
        }
        if(!user.getPhone().equals(result.getPhone())){
            flag = false;
        }
        if(!user.getAddress().equals(result.getAddress())){
            flag = false;
        }

        if(flag){//表示没有被更新
            CodeEnum.userdata_not_update.throwException();
        }

        //进行跟新
        result.setRealName(user.getRealName());
        result.setSex(user.getSex());
        result.setPhone(user.getPhone());
        result.setAddress(user.getAddress());

        int row = userMapper.updateById(result);
        if(row==0){
            CodeEnum.user_update_fail.throwException();
        }
        return true;
    }

    @Override
    public UserResponseDTO loginUser(String userName, String password) {
        //1. 根据用户名查询数据库(user)，检查用户是否注册
        User user = getUserByUserName(userName);
        //2. user==null   抛出异常未注册，前端页面跳转到注册页面
//        if(user == null){
//            throw new EnumException(CodeEnum.user_is_no_regist);
//        }
        CodeEnum.user_is_no_regist.assertIsNotNull(user); // 抛出用户未注异常


        // 3. 获取 密码， 通过user获取盐值，对dto中获取的密码进行加密
        String md5Password = MD5Util.getMd5Password(password, user.getSalt());

        //4. 加密后的密码和 user中的密码比较，如果不正确，抛出 密码错误异常
        if(!user.getPassword().equals(md5Password)){
            CodeEnum.password_is_incorrect.throwException();
        }

        UserResponseDTO ud = new UserResponseDTO();
        ud.setPkUid(user.getPkUid());
        ud.setRoleId(user.getRoleId());

        return ud; //返回角色ID
    }

    public User getUserByUserName(String userName){
        //根据用户名查询id和密码,盐值
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName).select("pk_uid","password","salt","role_id");

        User user = userMapper.selectOne(wrapper);
        return user;
    }

}




